﻿'use strict';

app.controller('ListController', function($scope, $resource,$stateParams,$state,resource,$modal) {
    
    var Dao = resource('article');
    $scope.queryModel = {
        page:$stateParams.page?parseInt($stateParams.page):1,
        pagesize:20
    };
    //$scope.pp = 2;
    //console.log(123,$stateParams.page,$scope.queryModel)
    $scope.search_context = $stateParams.search || '';
    $scope.query = function() {
        $scope.selected = false;
        
        //console.log('query',$stateParams)
        var search_context = $scope.search_context;
        
        var params = angular.extend({},{page:$scope.queryModel.page},$stateParams);
        Dao.invoke('list',params).then(function(result){
            var data = result.data;
            var list = data.list || [];
            var pager = data.pager;
            console.log(pager)
            $scope.pagerOption = pager;
            $scope.dataList = list;
            //$scope.queryModel.page = pager.page;
           
        });
    
    }
    $scope.del = function(id){
        dm.confirm('确认删除?',function(){
            Dao.invoke('delete',{id:id}).then(function(){
                Message.show('删除成功','success')
                scope.query();
            });
        });
    }
    
    $scope.clear = function(){
        $scope.queryModel = {
            page:1,
            pagesize:10
        };
    }
    //搜索跳转
    $scope.search = function(){
        var startDate = $scope.startDate;
        var endDate = $scope.endDate;
        var search_context = $scope.search_context;
        var page = $scope.queryModel.page;
        
        $state.go('app.news.list',{page:page,search:search_context,endDate:endDate,startDate:startDate});
    }
    //全选
    $scope.selected = false;
    $scope.selectAll = function(){
        $scope.selected = !$scope.selected;
        angular.forEach($scope.dataList,function(item){
            item.selected = $scope.selected;
        });
    }
    //自定义操作处理，其中1为删除所选记录
    $scope.exec = function(){
        if($scope.operate=="1"){
            var ids = [];
            angular.forEach($scope.dataList,function(item){
                if(item.selected){
                    ids.push(item.articleId);
                }
            });
            if(ids.length>0){
                //弹出删除确认
                dm.confirm('确认删除所选记录?',function(){
                  Dao.invoke('delete',{'ids':ids.join(',')}).then(function(){
                      Message.show('删除成功','success')
                      
                      $state.reload();
                  });
                });
                
                
            }
        }
    }
    // 显示编辑dialog
    $scope.toEditView = function(id){
      Dao.invoke('get',{id:id}).then(function(results){
        var data = results.data || {};
        
        $scope.data = angular.extend({},data);
      });
    };
    // 提交编辑数据
    $scope.submit = function(){
      Dao.invoke('update',$scope.data).then(function(results){
          Message.show('修改成功','success');
          $scope.query();
      });
    };

    $scope.query();
});


app.controller('DetailController', function($rootScope,$scope, resource, $stateParams,$state) {
  $scope.edit_mode = !!$stateParams.id;
  var $com = resource('article');
  if($scope.edit_mode){
      $com.invoke('get',{id:$stateParams.id}).then(function(results){
        var data = results.data || {};
        //console.log(1212,data)
        $scope.data = data;
      });
  }
  else{
      $scope.data = {};
  }
  $scope.submit = function(){
      if($scope.edit_mode){
          
          $com.invoke('update',$scope.data).then(function(results){
              Message.show('修改成功','success');
              $state.go($rootScope.previousState,$rootScope.previousStateParams);
          });
      }
      else{
          
          $com.invoke('create',$scope.data).then(function(results){
              Message.show('创建成功','success');
              $state.go('app.news.list');
          });
      }
  };
  
});